#include "StdAfx.h"
#include "LANChatMsgCtrl.h"

LANChatMsgCtrl::LANChatMsgCtrl(void* mainframe)
: m_mainframe(NULL)
{
	m_mainframe=(master*)mainframe;
	m_type="";
	m_ip="";
	m_nick="";
	m_num="";
	m_msg="";
}

LANChatMsgCtrl::~LANChatMsgCtrl(void)
{
}

void LANChatMsgCtrl::RoutingMsg(string msg)
{
	GetMsg(msg);
	if(m_type.compare("0")==0){
		SingleMessageParam* smp=new SingleMessageParam();
		char* nick=new char[m_nick.size()+1];
		memcpy(nick,m_nick.c_str(),m_nick.size()+1);
		char* ip=new char[m_ip.size()+1];
		memcpy(ip,m_ip.c_str(),m_ip.size()+1);
		char* msg=new char[m_msg.size()+1];
		memcpy(msg,m_msg.c_str(),m_msg.size()+1);
		smp->nick=nick;
		smp->ip=ip;
		smp->msg=msg;
		SendMessage(m_mainframe->GetSafeHwnd(),WM_SINGLEMESSAGE,sizeof(*smp),(LPARAM)smp);
		//m_mainframe->toitchatmsg(m_nick,m_ip,m_msg);
	}else if(m_type.compare("1")==0){
		MulitMessageParam* mmp=new MulitMessageParam();
		char* nick=new char[m_nick.size()+1];
		memcpy(nick,m_nick.c_str(),m_nick.size()+1);
		char* ip=new char[m_ip.size()+1];
		memcpy(ip,m_ip.c_str(),m_ip.size()+1);
		char* msg=new char[m_msg.size()+1];
		memcpy(msg,m_msg.c_str(),m_msg.size()+1);
		mmp->nick=nick;
		mmp->ip=ip;
		mmp->msg=msg;
		SendMessage(m_mainframe->GetSafeHwnd(),WM_MULITMESSAGE,sizeof(*mmp),(LPARAM)mmp);
	}else if(m_type.compare("2")==0){
		if(m_ip.compare(m_mainframe->lanchatsocket->getLoaclipaddr())!=0){
			CString nick(m_nick.c_str());
			CString ip(m_ip.c_str());
			user u;
			u.adduser(nick,ip);
			m_mainframe->pDialog[0]->friends.push_back(u);
			m_mainframe->pDialog[0]->m_list.InsertString(m_mainframe->pDialog[0]->total,nick);
			m_mainframe->pDialog[0]->total++;

			string msg="5^&";
			msg+=m_mainframe->lanchatsocket->getLoaclipaddr();
			msg+="^&";
			char* buf=::WCharToMByte(m_mainframe->myname.GetBuffer());
			string n(buf);
			msg+=n;
			msg+="^&1^&1";
			m_mainframe->lanchatsocket->Sendto(m_ip,msg);
		}
	}else if(m_type.compare("3")==0){
		CString nick(m_nick.c_str());
		m_mainframe->removefriend(nick);
	}else if(m_type.compare("4")==0){
		if(m_msg.compare("refuse")==0){
			::TerminateThread(m_mainframe->lanchatsocket->m_threadmap[m_ip],0);
		}else{
			/*m_mainframe->toitfilesend(m_nick,m_ip,m_msg);*/
			SingleMessageParam* smp=new SingleMessageParam();
			char* nick=new char[m_nick.size()+1];
			memcpy(nick,m_nick.c_str(),m_nick.size()+1);
			char* ip=new char[m_ip.size()+1];
			memcpy(ip,m_ip.c_str(),m_ip.size()+1);
			char* msg=new char[m_msg.size()+1];
			memcpy(msg,m_msg.c_str(),m_msg.size()+1);
			smp->nick=nick;
			smp->ip=ip;
			smp->msg=msg;
			SendMessage(m_mainframe->GetSafeHwnd(),WM_FILEMESSAGE,sizeof(*smp),(LPARAM)smp);
		}
	}else if(m_type.compare("5")==0){
		CString nick(m_nick.c_str());
		CString ip(m_ip.c_str());
		user u;
		u.adduser(nick,ip);
		m_mainframe->pDialog[0]->friends.push_back(u);
		m_mainframe->pDialog[0]->m_list.InsertString(m_mainframe->pDialog[0]->total,nick);
		m_mainframe->pDialog[0]->total++;
	}
}

void LANChatMsgCtrl::GetMsg(string msg)
{
	string next=msg;
	size_t index=next.find("^&");
	m_type=next.substr(0,index);
	next=next.substr(index+2);
	index=next.find("^&");
	m_ip=next.substr(0,index);
	next=next.substr(index+2);
	index=next.find("^&");
	m_nick=next.substr(0,index);
	next=next.substr(index+2);
	index=next.find("^&");
	m_num=next.substr(0,index);
	next=next.substr(index+2);
	m_msg=next;
}
